package com.cyc.cas;

import java.util.concurrent.atomic.AtomicInteger;

public class casDemo {

    //CAS 比较并交换
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(2020);
        //期望  更新
        // 如果期望的值达到了 就更新 否则 就不更新  CAS是CPU的并发原语
        System.out.println(atomicInteger.compareAndSet(2020, 2021));//true
        System.out.println(atomicInteger.get()); //2021

        System.out.println(atomicInteger.compareAndSet(2020, 2021));//false
        System.out.println(atomicInteger.get()); //2021
    }
}
